<template>
  <PageWrapper dense fixedHeight contentFullHeight>
    <SupplierInfo :id="recordId" @return-page="returnPage" v-if="showPageType === 1" />
    <RiskAssessment
      :isView="isView"
      :id="recordId"
      :isAnnual="true"
      @return-page="returnPage"
      v-else-if="showPageType === 2 || showPageType === 4"
    />
    <SupplierInfoList
      :id="recordId"
      :name="recordName"
      @return-page="returnPage"
      v-if="showPageType === 3"
    />
    <BasicTable @register="registerTable" v-else>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'formalState'">
          <span>{{ getFormalState(record.formalState) }}</span>
        </template>
        <template v-if="column.dataIndex == 'type' && !!record.type">
          <a-tag
            v-for="(item, index) in record.type.split(',')"
            :key="index"
            :color="getType(item, false)"
          >
            {{ getType(item, true) }}
          </a-tag>
        </template>
        <template v-if="column.dataIndex == 'action'">
          <a-button type="link" class="actionTxt" @click="handleEdit(record)">编辑</a-button>
          <a-button type="link" class="actionTxt" @click="showPage(1, record)">详情查看</a-button>
          <a-button
            type="link"
            class="actionTxt"
            @click="showPage(2, record)"
            :disabled="record.formalState === 4"
            v-if="record.formalState !== 3"
          >
            企业年审
          </a-button>
          <a-button
            type="link"
            class="actionTxt"
            @click="showPage(4, record)"
            v-if="record.formalState === 3"
          >
            年审查看
          </a-button>
          <a-button type="link" class="actionTxt" @click="showPage(3, record)">供货清单</a-button>
          <a-button type="link" class="actionTxt" @click="handleEliminate(record)">淘汰</a-button>
          <a-button type="link" class="actionTxt" @click="handleDelete(record)">删除</a-button>
        </template>
      </template>
    </BasicTable>
    <PotentialModal @register="registerModal" @success="reload" />
    <EliminateModal @register="registerEliminateModal" @success="reload" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { createVNode, ref, onMounted } from 'vue';
  import { BasicTable, useTable, BasicColumn, FormSchema } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { getSupplierPageList, deleteSupplier } from '/@/api/erp/supplier/list';
  import { getDicDetailList } from '/@/api/system/dic';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import PotentialModal from './components/PotentialModal.vue';
  import RiskAssessment from './components/RiskAssessment.vue';
  import EliminateModal from './components/EliminateModal.vue';
  import SupplierInfo from './components/SupplierInfo.vue';
  import SupplierInfoList from './components/SupplierInfoList.vue';

  const { t } = useI18n();
  const columns: BasicColumn[] = [
    {
      title: '供应商编码',
      dataIndex: 'number',
      width: 150,
    },
    {
      title: '供应商名称',
      dataIndex: 'name',
    },
    {
      title: '供应商负责人',
      dataIndex: 'person',
      width: 100,
    },
    {
      title: '手机号',
      dataIndex: 'phone',
      width: 100,
    },
    {
      title: '经营范围',
      dataIndex: 'scope',
    },
    {
      title: '当前年审',
      dataIndex: 'formalState',
      width: 100,
    },
    {
      title: '供应商类型',
      dataIndex: 'type',
      width: 220,
    },
  ];

  const searchFormSchema: FormSchema[] = [
    {
      field: 'formalState',
      label: '当前年审',
      component: 'XjrSelect',
      colProps: { span: 8 },
      componentProps: {
        datasourceType: 'dic',
        params: { itemId: '1678217311978692610' },
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择当前年审',
        getPopupContainer: () => document.body,
      },
    },
    {
      field: 'type',
      label: '供应商类别',
      component: 'XjrSelect',
      colProps: { span: 8 },
      componentProps: {
        datasourceType: 'dic',
        params: { itemId: '1676483934342787074' },
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择供应商类别',
        getPopupContainer: () => document.body,
      },
    },
    {
      field: 'name',
      label: '供应商名称',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入供应商名称',
      },
    },
  ];

  const { notification } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerEliminateModal, { openModal: openEliminateModal }] = useModal();

  const recordId = ref('');
  const recordName = ref('');
  const showPageType = ref<any>('');
  const isView = ref(false);
  const typeOptions = ref<Recordable[]>([]);
  const [registerTable, { reload }] = useTable({
    title: '正式供应商',
    api: getSupplierPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
    },
    beforeFetch: (params) => {
      return { ...params, state: 1 };
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 320,
      title: t('操作'),
      dataIndex: 'action',
    },
  });

  onMounted(async () => {
    typeOptions.value = await getDicDetailList({ itemId: '1676483934342787074' });
  });

  const getFormalState = (state) => {
    switch (state) {
      case 4:
        return '不需要年审';
      case 3:
        return '已年审';
      default:
        return '未年审';
    }
  };

  const getType = (type, isText) => {
    const data = typeOptions.value.filter((x) => x.id === type);
    if (!data.length) return '';
    if (isText) return data[0].name;
    switch (data[0].name) {
      case '原料':
        return 'processing';
      case '成品':
        return 'error';
      case '半成品':
        return 'success';
      case '其它':
        return 'warning';
    }
  };

  const handleEdit = (record) => {
    openModal(true, {
      id: record.id,
      isUpdate: true,
      title: '正式',
    });
  };

  const handleEliminate = (record) => {
    openEliminateModal(true, {
      id: record.id,
      name: record.name,
      number: record.number,
    });
  };

  const handleDelete = (record) => {
    Modal.confirm({
      title: t('提示信息'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('是否确认删除？'),
      okText: t('确认'),
      cancelText: t('取消'),
      async onOk() {
        await deleteSupplier(record.id);
        notification.success({
          message: t('提示'),
          description: t('删除成功'),
        });
        reload();
      },
      onCancel() {},
    });
  };

  const showPage = (type, record) => {
    isView.value = type === 4;
    showPageType.value = type;
    recordId.value = record.id;
    recordName.value = record.name;
  };

  const returnPage = () => {
    showPageType.value = '';
    reload();
  };
</script>
<style lang="less" scoped>
  .actionTxt {
    padding: 4px 2px;

    &:last-child {
      color: #f56c6c;
    }
  }

  :deep(.ant-tag) {
    margin-right: 5px;
  }
</style>
